﻿using System;
using System.Windows;
using System.Windows.Threading;
using KinectCurve.Entities;
using GalaSoft.MvvmLight.Messaging;

namespace KinectCurve.ViewModel
{
    public partial class MainViewModel
    {
        public void StartTimer()
        {
            //start game timer
            _gameStartTime = DateTime.Now;

            IsGameInProgress = true;
            IsGameInProgressVisibility = Visibility.Visible;
            
            //create dispatcher timer
            _gameTimer = new DispatcherTimer();
            _gameTimer.Interval = TimeSpan.FromSeconds(1);
            _gameTimer.Tick += (s, e) =>
            {
                //update game time
                var elapsed = DateTime.Now.Subtract(_gameStartTime);
                var currentTime = new DateTime(elapsed.Ticks);
                TimeToPlay = currentTime.ToString("mm:ss");

                //after 1 minutes of game time stop the game
                if (elapsed.Seconds > Settings.ExpireTime) StopTimer();
                //ViewModelLocator.GameStatic.UpdateGame();
                //ViewModelLocator.GameStatic.Draw();

            };
            _gameTimer.Start();
        }

        public void StopTimer()
        {
            IsGameInProgress = false;
            IsGameInProgressVisibility = Visibility.Collapsed;

            Messenger.Default.Send<EnumGameEnd>(EnumGameEnd.TimeExpired, "endGame");
            Messenger.Default.Send<EnumMenu>(EnumMenu.Menu, "menuCommand");

            //stop game timer
            if (_gameTimer != null)
            {
                _gameTimer.Stop();
                _gameTimer = null;
            }
        }
    }
}
